package com.zjun.demo.experimentaldemo.scale_seekBar

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatSeekBar


/**
 * 蜂窝颜色选择器
 */
class SimpleScaleSeekBarView : AppCompatSeekBar {


    /**
     * 绘制的类型 0为圆点，1为刻度
     */
    private var drawStyle = 1

    /**
     * 是否绘制文字
     */
    private var drawText = false

    /**
     * 刻度线画笔
     */
    private lateinit var mRulerPaint: Paint

    /**
     * 圆点画笔
     */
    private lateinit var mCirclePaint: Paint

    /**
     * 字体画笔
     */
    private lateinit var mTestPaint: Paint

    /**
     * 绘制的个数,等分数等于刻度线的个数加1
     */
    private var mCount = 10

    /**
     * 每条刻度线的宽度
     */
    private var mRulerWidth = 2

    /**
     * 刻度线的颜色
     */
    private var mRulerColor = Color.WHITE

    /**
     * 圆点的颜色
     */
    private var mCircleColor = Color.WHITE

    /**
     * 字体的颜色
     */
    private var mTextColor = Color.WHITE

    /**
     * 滑块上面是否要显示
     */
    private var isShowTopOfThumb = false

    constructor(context: Context) : super(context) {
        initialization()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initialization(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initialization(attrs)
    }

    @SuppressLint("ObsoleteSdkInt")
    private fun initialization(attrs: AttributeSet? = null) {
        //创建绘制刻度线的画笔
        mRulerPaint = Paint()
        mRulerPaint.color = mRulerColor;
        mRulerPaint.isAntiAlias = true;

        //创建绘制圆点的画笔
        mCirclePaint = Paint()
        mCirclePaint.color = mCircleColor
        mCirclePaint.isAntiAlias = true

        //创建绘制文字的画笔
        mTestPaint = Paint()
        mTestPaint.textSize = 35F;
        mTestPaint.color = mTextColor
        mTestPaint.isAntiAlias = true

        //Api21及以上调用，去掉滑块后面的背景
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            splitTrack = false
        }
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //极限条件校验

        //极限条件校验
        if (width <= 0 || mCount <= 0) {
            return
        }
        //获取每一份的长度
        //获取每一份的长度
        var length = 0
        if (drawStyle == 0) {
            length = width / (mCount + 1)
        } else if (drawStyle == 1 || drawStyle == 2) {
            length = (width - paddingLeft - paddingRight - mCount * mRulerWidth) / (mCount + 1)
        }

        //计算刻度线的顶部坐标和底部坐标 注意绘制刻度的时候要设置minHeight
        val rulerTop = height / 2 - minimumHeight / 2
        val rulerBottom = rulerTop + minimumHeight

        val drawY = ((rulerTop + rulerBottom) / 2).toFloat()
        val drawTestY = ((rulerTop + rulerBottom) / 2).toFloat() - 50
        //获取滑块的位置信息
        //获取滑块的位置信息
        var thumbRect: Rect? = null
        if (thumb != null) {
            thumbRect = thumb.bounds
        }

        var rulerLeft = 0
        var rulerRight = 0
        var drawX = 0f

        //绘制刻度线

        //绘制刻度线
        for (i in 1..mCount) {
            //计算刻度线的左边坐标和右边坐标
            if (drawStyle == 0) {
                rulerLeft = i * length
                rulerRight = rulerLeft
                drawX = rulerLeft.toFloat()
            } else if (drawStyle == 1 || drawStyle == 2) {
                rulerLeft = i * length + paddingLeft
                rulerRight = rulerLeft + mRulerWidth
                drawX = ((rulerLeft + rulerRight) / 2).toFloat()
            }
            val drawTestX = (rulerLeft - 10).toFloat()
            //判断是否需要绘制刻度线
            if (!isShowTopOfThumb && thumbRect != null && rulerLeft - paddingLeft > thumbRect.left && rulerRight - paddingLeft < thumbRect.right) {
                continue
            }
            //进行绘制
            if (drawStyle == 0) {
                canvas.drawCircle(drawX, drawY, 8.0f, mCirclePaint)
            } else if (drawStyle == 1) {
                canvas.drawRect(rulerLeft.toFloat(), rulerTop.toFloat(), rulerRight.toFloat(), rulerBottom.toFloat(), mRulerPaint)
            }else if (drawStyle == 2) {
                canvas.drawRect(rulerLeft.toFloat(), rulerTop.toFloat()+(rulerBottom - rulerTop)/3*2, rulerRight.toFloat(), rulerBottom.toFloat(), mRulerPaint)
            }
            //是否绘制文字
            if (drawText) {
                canvas.drawText((i - 1).toString(), drawTestX, drawTestY, mTestPaint)
            }
        }
    }


    /**
     * 设置绘制的个数
     *
     * @param mCount
     */
    fun setCount(mCount: Int) {
        this.mCount = mCount
        requestLayout()
    }

    /**
     * 设置绘制类型
     *
     * @param mStyle
     */
    fun setDrawStyle(mStyle: Int) {
        drawStyle = mStyle
        requestLayout()
    }

    /**
     * 设置是否绘制文字
     *
     * @param drawText
     */
    fun setDrawText(drawText: Boolean) {
        this.drawText = drawText
        requestLayout()
    }

    /**
     * 设置刻度线的宽度，单位(px)
     *
     * @param mRulerWidth
     */
    fun setRulerWidth(mRulerWidth: Int) {
        this.mRulerWidth = mRulerWidth
        requestLayout()
    }

    /**
     * 设置刻度线的颜色
     *
     * @param mRulerColor
     */
    fun setRulerColor(mRulerColor: Int) {
        this.mRulerColor = mRulerColor
        if (mRulerPaint != null) {
            mRulerPaint.color = mRulerColor
            requestLayout()
        }
    }

    /**
     * 设置圆点的颜色
     *
     * @param mCircleColor
     */
    fun setCircleColor(mCircleColor: Int) {
        this.mCircleColor = mCircleColor
        if (mCirclePaint != null) {
            mCirclePaint.color = mCircleColor
            requestLayout()
        }
    }

    /**
     * 设置字体的颜色
     *
     * @param TextColor
     */
    fun setTextColor(TextColor: Int) {
        mTextColor = TextColor
        if (mTestPaint != null) {
            mTestPaint.color = mTextColor
            requestLayout()
        }
    }

    /**
     * 设置整体颜色
     *
     * @param mCircleColor,mTextColor
     */
    fun setAllColor(mCircleColor: Int, mTextColor: Int) {
        //圆的颜色
        this.mCircleColor = mCircleColor
        //字体的颜色
        this.mTextColor = mTextColor
        if (mCirclePaint != null && mTestPaint != null) {
            mCirclePaint.color = mCircleColor
            mTestPaint.color = mTextColor
            requestLayout()
        }
    }

    /**
     * 滑块上面是否需要显示刻度线
     *
     * @param isShowTopOfThumb
     */
    fun setShowTopOfThumb(isShowTopOfThumb: Boolean) {
        this.isShowTopOfThumb = isShowTopOfThumb
        requestLayout()
    }


}